package net.chenlin.dp.modules.business.service.impl;

import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.Query;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.modules.business.dao.DistrictManageMapper;
import net.chenlin.dp.modules.business.dao.LineChartMapper;
import net.chenlin.dp.modules.business.dao.TcpDataMapper;
import net.chenlin.dp.modules.business.dao.TestManageMapper;
import net.chenlin.dp.modules.business.entity.dto.LineChartQueryDto;
import net.chenlin.dp.modules.business.entity.vo.*;
import net.chenlin.dp.modules.business.entity.vo.device.DeviceTreeVo;
import net.chenlin.dp.modules.business.entity.vo.device.DeviceVo;
import net.chenlin.dp.modules.business.entity.vo.districtManage.DistrictTreeVo;
import net.chenlin.dp.modules.business.entity.vo.lineChart.*;
import net.chenlin.dp.modules.business.entity.vo.lineLossAnalysis.DeviceHistoryDataVo;
import net.chenlin.dp.modules.business.entity.vo.lineLossAnalysis.LineLossAnalysisListVo;
import net.chenlin.dp.modules.business.service.LineChartService;
import net.chenlin.dp.modules.utils.ArrayUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Yangsm
 * @ClassName: LineChartServiceImpl
 * @Description: TODO
 * @date 2022年11月02日 22:00
 */
@Service
public class LineChartServiceImpl implements LineChartService {

    @Autowired
    private LineChartMapper lineChartMapper;

    @Autowired
    private TestManageMapper testManageMapper;

    @Autowired
    private TcpDataMapper tcpDataMapper;

    @Autowired
    private DistrictManageMapper districtManageMapper;

    @Override
    public R getLineChart(LineChartQueryDto queryDto) {
        LineChartVo lineChartVo = new LineChartVo();

        List<String> electricNoList = new ArrayList<>();

        // 根据试验获取下面你的设备编号
        List<String> deviceList = testManageMapper.getDeviceList(queryDto.getTestNo());
        if (deviceList.size() == 0) {
            return R.error("未获取到该实验下的设备编号！");
        }

        if (queryDto.getPassNo() != 0) {
            // 获取第一个设备的电表信息
            LineChartQueryDto newQuery = new LineChartQueryDto();
            newQuery.setTestNo(queryDto.getTestNo());
            newQuery.setDeviceNo(deviceList.get(0));
            newQuery.setPassNo(queryDto.getPassNo());

            // 根据试验编号以及设备信息以及
            String electricNo = lineChartMapper.getElectricNo(newQuery);
            if (StringUtils.isEmpty(electricNo)) {
                return R.error("未获取到该实验设备下通道的电表号！");
            }
            String[] split = electricNo.split(",");
            electricNoList = Arrays.asList(split);
        }

        if (queryDto.getPassNo() == 0) {

            List<DeviceVo> deviceVoList = lineChartMapper.getDeviceListByTestNo(queryDto.getTestNo());
            if (deviceVoList.size() == 0) {
                return R.error("未获取到该实验下未知通道下的设备编号！");
            }
            for (int i = 0; i < deviceVoList.size(); i++) {
                String electricNo = deviceVoList.get(i).getElectricNo();
                String[] split = electricNo.split(",");
                electricNoList = Arrays.asList(split);
                //获取每个通道 时间参数对应电表的电量
//                Double channelPower = lineChartMapper.getAllTotal(queryDto.getStartDate(), queryDto.getEndDate(), electricNoList);
//                if (channelPower != null) {
//                    // 在tcp获取每个设备的电量
//
//                    AllTcpTotalVo allTcpTotalVo = lineChartMapper.getAllTcpTotal(deviceVoList.get(i).getDeviceNo(), queryDto.getStartDate(), queryDto.getEndDate());
//                    if (allTcpTotalVo == null) {
//                        return R.error("当前时间条件下未找到TCP推送数据！");
//                    }
//
//                    Integer passNo = ArrayUtils.compareNum(channelPower, allTcpTotalVo);
//                    queryDto.setPassNo(passNo);
//                } else {
//                    System.out.println(" ==== 当前参数条件下未找到excel导入数据！");
//                }
            }

        }

        // tcp数据
        List<LineChartDataVo> typeOneList = new ArrayList<>();

        // excel数据
        List<LineChartDataVo> typeTwoList = new ArrayList<>();

        SimpleDateFormat fierformatter = new SimpleDateFormat("HH:mm:ss");

        if (deviceList.size() > 1) {
            // 多设备逻辑（最多两个设备现在）
            // 根据选取的通道信息（可能是单选或者选所有）
            // 如果是单选的

            // 获取每个设备
            LineChartQueryDto tcpQuery = new LineChartQueryDto();
            tcpQuery.setStartDate(queryDto.getStartDate());
            tcpQuery.setEndDate(queryDto.getEndDate());
            tcpQuery.setDeviceNo(deviceList.get(0));
            // 获取设备1的TCP数据
            List<DataMessageVo> d1TcpDataList = lineChartMapper.getTcpDataList(tcpQuery);
            if (d1TcpDataList.size() == 0) {
                return R.error("未获取到当前查询时间段设备1的TCP数据");
            }
            tcpQuery.setDeviceNo(deviceList.get(1));

            // 获取设备2的TCP数据
            List<DataMessageVo> d2TcpDataList = lineChartMapper.getTcpDataList(tcpQuery);

            if (d2TcpDataList.size() == 0) {
                return R.error("未获取到当前查询时间段设备2的TCP数据");
            }

            //封装TCP通道的数据
            for (int i = 0; i < d1TcpDataList.size(); i++) {

                SplicingChannelVo splicingChannelVo = new SplicingChannelVo();
                splicingChannelVo.setMeasurementStartTime(d1TcpDataList.get(i).getMeasurementStartTime());
                splicingChannelVo.setMeasurementEndTime(d1TcpDataList.get(i).getMeasurementEndTime());
                if (queryDto.getPassNo() == 1) {
                    if (i == 0) {
                        LineChartDataVo fiertVo = new LineChartDataVo();

                        String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int index = fierformat.lastIndexOf(":");
                        fierformat = fierformat.substring(0, index);
                        fiertVo.setTime(fierformat);
                        fiertVo.setDifference("0.00");
                        typeOneList.add(fiertVo);
                    } else {
                        Double d1OnePower = d1TcpDataList.get(i).getOneChannelAccumulatedPower();
                        Double d2OnePower = d2TcpDataList.get(i).getOneChannelAccumulatedPower();

                        BigDecimal b1 = new BigDecimal(Double.toString(d1OnePower));
                        BigDecimal b2 = new BigDecimal(Double.toString(d2OnePower));
                        double res = Math.abs(b1.subtract(b2).doubleValue());

                        Double d1OnePower1 = d1TcpDataList.get(i - 1).getOneChannelAccumulatedPower();
                        Double d2OnePower2 = d2TcpDataList.get(i - 1).getOneChannelAccumulatedPower();

                        BigDecimal b11 = new BigDecimal(Double.toString(d1OnePower1));
                        BigDecimal b22 = new BigDecimal(Double.toString(d2OnePower2));
                        double res2 = Math.abs(b11.subtract(b22).doubleValue());

                        BigDecimal res3 = new BigDecimal(Double.toString(res));
                        BigDecimal res4 = new BigDecimal(Double.toString(res2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        //splicingChannelVo.setChannelPower(result);

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                }
                if (queryDto.getPassNo() == 2) {
                    if (i == 0) {
                        LineChartDataVo fiertVo = new LineChartDataVo();

                        String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int index = fierformat.lastIndexOf(":");
                        fierformat = fierformat.substring(0, index);
                        fiertVo.setTime(fierformat);
                        fiertVo.setDifference("0.00");
                        typeOneList.add(fiertVo);
                    } else {
                        Double d1OnePower = d1TcpDataList.get(i).getTwoChannelAccumulatedPower();
                        Double d2OnePower = d2TcpDataList.get(i).getTwoChannelAccumulatedPower();

                        BigDecimal b1 = new BigDecimal(Double.toString(d1OnePower));
                        BigDecimal b2 = new BigDecimal(Double.toString(d2OnePower));
                        double res = Math.abs(b1.subtract(b2).doubleValue());

                        Double d1OnePower1 = d1TcpDataList.get(i - 1).getTwoChannelAccumulatedPower();
                        Double d2OnePower2 = d2TcpDataList.get(i - 1).getTwoChannelAccumulatedPower();

                        BigDecimal b11 = new BigDecimal(Double.toString(d1OnePower1));
                        BigDecimal b22 = new BigDecimal(Double.toString(d2OnePower2));
                        double res2 = Math.abs(b11.subtract(b22).doubleValue());

                        BigDecimal res3 = new BigDecimal(Double.toString(res));
                        BigDecimal res4 = new BigDecimal(Double.toString(res2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        //splicingChannelVo.setChannelPower(result);

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }

                }
                if (queryDto.getPassNo() == 3) {
                    if (i == 0) {
                        LineChartDataVo fiertVo = new LineChartDataVo();

                        String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int index = fierformat.lastIndexOf(":");
                        fierformat = fierformat.substring(0, index);
                        fiertVo.setTime(fierformat);
                        fiertVo.setDifference("0.00");
                        typeOneList.add(fiertVo);
                    } else {
                        Double d1OnePower = d1TcpDataList.get(i).getThreeChannelAccumulatedPower();
                        Double d2OnePower = d2TcpDataList.get(i).getThreeChannelAccumulatedPower();

                        BigDecimal b1 = new BigDecimal(Double.toString(d1OnePower));
                        BigDecimal b2 = new BigDecimal(Double.toString(d2OnePower));
                        double res = Math.abs(b1.subtract(b2).doubleValue());

                        Double d1OnePower1 = d1TcpDataList.get(i - 1).getThreeChannelAccumulatedPower();
                        Double d2OnePower2 = d2TcpDataList.get(i - 1).getThreeChannelAccumulatedPower();

                        BigDecimal b11 = new BigDecimal(Double.toString(d1OnePower1));
                        BigDecimal b22 = new BigDecimal(Double.toString(d2OnePower2));
                        double res2 = Math.abs(b11.subtract(b22).doubleValue());

                        BigDecimal res3 = new BigDecimal(Double.toString(res));
                        BigDecimal res4 = new BigDecimal(Double.toString(res2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        //splicingChannelVo.setChannelPower(result);

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                }

                System.out.println("  ======================获取设备下面的电表的电量总和===========================================");

                // 获取第一个设备的电表信息
                LineChartQueryDto newQuery = new LineChartQueryDto();
                newQuery.setTestNo(queryDto.getTestNo());
                newQuery.setDeviceNo(deviceList.get(0));
                newQuery.setPassNo(queryDto.getPassNo());

                // 根据试验编号以及设备信息以及
                String electricNo = lineChartMapper.getElectricNo(newQuery);
                if (StringUtils.isEmpty(electricNo)) {
                    return R.error("未获取到该实验设备下通道的电表号!");
                }
                String[] split = electricNo.split(",");
                List<String> threeMealsList = Arrays.asList(split);

                // 获取第、二个设备的电表信息
                newQuery.setDeviceNo(deviceList.get(1));
                // 根据试验编号以及设备信息以及
                String electricNo2 = lineChartMapper.getElectricNo(newQuery);
                if (StringUtils.isEmpty(electricNo2)) {
                    return R.error("未获取到该实验设备下通道的电表号!");
                }
                String[] split2 = electricNo2.split(",");
                List<String> threeMealsList2 = Arrays.asList(split2);

                if (i == 0) {
                    LineChartDataVo fiertVo = new LineChartDataVo();

                    String fierformat = fierformatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                    int index = fierformat.lastIndexOf(":");
                    fierformat = fierformat.substring(0, index);
                    fiertVo.setTime(fierformat);
                    fiertVo.setDifference("0.00");
                    typeTwoList.add(fiertVo);
                } else {
                    // 根据电表号以及结束时间获取该通道下面电量何
                    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String format1 = form.format(d1TcpDataList.get(i).getMeasurementEndTime());
                    System.out.println(format1);
                    String format2 = form.format(d1TcpDataList.get(i - 1).getMeasurementEndTime());
                    System.out.println(format2);

                    Double channelPower = lineChartMapper.getTimeTotal(format1, threeMealsList);
                    // 根据电表号以及结束时间获取该通道下面电量何
                    Double channelPower2 = lineChartMapper.getTimeTotal(format1, threeMealsList2);
                    if (channelPower != null && channelPower2 != null) {
                        BigDecimal res3 = new BigDecimal(Double.toString(channelPower));
                        BigDecimal res4 = new BigDecimal(Double.toString(channelPower2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        // 根据电表号以及结束时间获取该通道下面电量何
                        Double channelPower3 = lineChartMapper.getTimeTotal(format2, threeMealsList);

                        // 根据电表号以及结束时间获取该通道下面电量何
                        Double channelPower4 = lineChartMapper.getTimeTotal(format2, threeMealsList2);

                        if (channelPower3 != null && channelPower4 != null) {
                            BigDecimal res5 = new BigDecimal(Double.toString(channelPower3));
                            BigDecimal res6 = new BigDecimal(Double.toString(channelPower4));
                            double result2 = Math.abs(res5.subtract(res6).doubleValue());

                            BigDecimal res7 = new BigDecimal(Double.toString(result));
                            BigDecimal res8 = new BigDecimal(Double.toString(result2));
                            double result3 = Math.abs(res7.subtract(res8).doubleValue());

                            LineChartDataVo vo = new LineChartDataVo();
                            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                            String format = formatter.format(d1TcpDataList.get(i).getMeasurementEndTime());
                            int i1 = format.lastIndexOf(":");
                            format = format.substring(0, i1);
                            vo.setTime(format);
                            String douStr = String.format("%.2f", result3);
                            if (douStr.contains("-")) {
                                douStr = douStr.replace("-", "");
                            }
                            vo.setDifference(douStr);
                            typeTwoList.add(vo);
                        }
                    }
                }
            }
        }

        // 单个设备
        if (deviceList.size() == 1) {
            LineChartQueryDto tcpQuery = new LineChartQueryDto();
            tcpQuery.setStartDate(queryDto.getStartDate());
            tcpQuery.setEndDate(queryDto.getEndDate());
            tcpQuery.setDeviceNo(deviceList.get(0));
            // 获取设备1的TCP数据
            List<DataMessageVo> tcpDataList = lineChartMapper.getTcpDataList(tcpQuery);

            //封装TCP通道的数据
            for (int i = 0; i < tcpDataList.size(); i++) {

                SplicingChannelVo splicingChannelVo = new SplicingChannelVo();
                splicingChannelVo.setMeasurementStartTime(tcpDataList.get(i).getMeasurementStartTime());
                splicingChannelVo.setMeasurementEndTime(tcpDataList.get(i).getMeasurementEndTime());

                if (i == 0) {

                    LineChartDataVo fiertVo = new LineChartDataVo();

                    String fierformat = fierformatter.format(tcpDataList.get(i).getMeasurementEndTime());
                    int index = fierformat.lastIndexOf(":");
                    fierformat = fierformat.substring(0, index);
                    fiertVo.setTime(fierformat);
                    fiertVo.setDifference("0.00");
                    typeOneList.add(fiertVo);
                    typeTwoList.add(fiertVo);

                } else {
                    if (queryDto.getPassNo() == 1) {
                        Double onePower = tcpDataList.get(i).getOneChannelAccumulatedPower();
                        Double onePower1 = tcpDataList.get(i - 1).getOneChannelAccumulatedPower();

                        BigDecimal res3 = new BigDecimal(Double.toString(onePower));
                        BigDecimal res4 = new BigDecimal(Double.toString(onePower1));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);

                    }
                    if (queryDto.getPassNo() == 2) {
                        Double onePower = tcpDataList.get(i).getTwoChannelAccumulatedPower();
                        Double onePower1 = tcpDataList.get(i - 1).getTwoChannelAccumulatedPower();

                        BigDecimal res3 = new BigDecimal(Double.toString(onePower));
                        BigDecimal res4 = new BigDecimal(Double.toString(onePower1));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                    if (queryDto.getPassNo() == 3) {
                        Double onePower = tcpDataList.get(i).getThreeChannelAccumulatedPower();
                        Double onePower1 = tcpDataList.get(i - 1).getThreeChannelAccumulatedPower();

                        BigDecimal res3 = new BigDecimal(Double.toString(onePower));
                        BigDecimal res4 = new BigDecimal(Double.toString(onePower1));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeOneList.add(vo);
                    }
                }

                System.out.println("  ======================获取设备下面的电表的电量总和===========================================");


                if (i != 0) {
                    // 根据电表号以及结束时间获取该通道下面电量何
                    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String format1 = form.format(tcpDataList.get(i).getMeasurementEndTime());
                    System.out.println(format1);
                    String format2 = form.format(tcpDataList.get(i - 1).getMeasurementEndTime());
                    System.out.println(format2);
                    Double channelPower = lineChartMapper.getTimeTotal(format1, electricNoList);
                    Double channelPower2 = lineChartMapper.getTimeTotal(format2, electricNoList);
                    if (channelPower != null && channelPower2 != null) {
                        BigDecimal res3 = new BigDecimal(Double.toString(channelPower));
                        BigDecimal res4 = new BigDecimal(Double.toString(channelPower2));
                        double result = Math.abs(res3.subtract(res4).doubleValue());

                        LineChartDataVo vo = new LineChartDataVo();
                        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                        String format = formatter.format(tcpDataList.get(i).getMeasurementEndTime());
                        int i1 = format.lastIndexOf(":");
                        format = format.substring(0, i1);
                        vo.setTime(format);
                        String douStr = String.format("%.2f", result);
                        if (douStr.contains("-")) {
                            douStr = douStr.replace("-", "");
                        }
                        vo.setDifference(douStr);
                        typeTwoList.add(vo);
                    }
                }
            }
        }
        lineChartVo.setTypeOne(typeOneList);
        lineChartVo.setTypeTwo(typeTwoList);
        return CommonUtils.msg(lineChartVo);
    }

    @Override
    public R getLineChartQueryTime(String testNo) {
        LineChartQueryTimeVo vo = new LineChartQueryTimeVo();
        List<Date> dateList = new ArrayList<>();
        // 获取设备信息
        List<String> deviceNoList = lineChartMapper.getDeviceNo(testNo);
        if (deviceNoList.size() == 0) {
            return R.error("未获取到该实验的设备信息!");
        }
        // 获取TCP的时间
        deviceNoList.stream().forEach(deviceNo -> {
            Date minDate = lineChartMapper.getMinDate(deviceNo);
            Date maxDate = lineChartMapper.getMaxDate(deviceNo);
            if (minDate != null) {
                dateList.add(minDate);
            }
            if (maxDate != null) {
                dateList.add(maxDate);
            }

            // 获取当前设备下的所有通道编号
            List<String> passNoList = lineChartMapper.getPassNoList(testNo, deviceNo);

            // 根据编号获取电表号
            passNoList.stream().forEach(passNo -> {
                String eleNo = lineChartMapper.getEleNo(testNo, deviceNo, passNo);
                if (StringUtils.isNotEmpty(eleNo)) {
                    // 根据电表号获取电量信息确认参数的默认通道
                    String[] split = eleNo.split(",");
                    List<String> eleList = Arrays.asList(split);
                    for (int i = 0; i < eleList.size(); i++) {
                        Integer count = lineChartMapper.getExcelData(eleList.get(i));
                        if (count > 0) {
                            vo.setPassNo(passNo);
                        }
                    }
                }
            });
        });

        if (dateList.size() != 0) {
            Date min = Collections.min(dateList);
            Date max = Collections.max(dateList);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            vo.setStartDate(sdf.format(min));
            vo.setEndDate(sdf.format(max));
        }

        if (vo.getStartDate() == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(new Date());
            vo.setStartDate(format);
        }

        if (vo.getPassNo() == null) {
            vo.setPassNo("0");
        }

        if (vo.getEndDate() == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(new Date());
            vo.setEndDate(format);
        }

        return CommonUtils.msg(vo);
    }

    @Override
    public Page<LineLossAnalysisListVo> getLineLossAnalysisList(Map<String, Object> params) {
        Query query = new Query(params);
        Page<LineLossAnalysisListVo> page = new Page<>(query);
        List<LineLossAnalysisListVo> list = lineChartMapper.getLineLossAnalysisList(page, query);
        list.stream().forEach(listVo -> {
            // 获取台区名称
            String name = lineChartMapper.getDistrictName(listVo.getDeviceNo());
            Integer districtId = lineChartMapper.getDistrictId(listVo.getDeviceNo());
            listVo.setDistrictName(name);

            // 获取设备校准系数
            BigDecimal A_calibrationFactor = new BigDecimal(1.00);
            BigDecimal B_calibrationFactor = new BigDecimal(1.00);
            BigDecimal C_calibrationFactor = new BigDecimal(1.00);

            // 根据设备ID获取设备校准系数
            if(StringUtils.isNotEmpty(listVo.getDeviceNo())){
                List<BigDecimal> calibrationFactorList = tcpDataMapper.getCalibrationFactor(listVo.getDeviceNo());
                for (int i = 0; i < calibrationFactorList.size(); i++) {
                    BigDecimal bigDecimal = calibrationFactorList.get(i);
                    if(i == 0){
                        A_calibrationFactor = bigDecimal;
                    }
                    if(i == 1){
                        B_calibrationFactor = bigDecimal;
                    }
                    if(i == 2){
                        C_calibrationFactor = bigDecimal;
                    }
                }
            }

            if (listVo.getDeviceStatus() == 1) {
                listVo.setMonitoringStartTime(null);
                return;
            } else {

                List<DateNumListVo> dateList = new ArrayList<>();
                // 2024/1/3 修改线损分析乘以校准系数
                List<DateNumListVo> adateList = lineChartMapper.getAphaseSequenceDateList(listVo.getDeviceNo(), listVo.getMonitoringStartTime());
                List<DateNumListVo> bdateList = lineChartMapper.getBphaseSequenceDateList(listVo.getDeviceNo(), listVo.getMonitoringStartTime());
                List<DateNumListVo> cdateList = lineChartMapper.getCphaseSequenceDateList(listVo.getDeviceNo(), listVo.getMonitoringStartTime());
                if (CollectionUtils.isEmpty(adateList)){
                    return;
                }
                for (int i = 0; i < adateList.size(); i++) {
                    BigDecimal amultiply = BigDecimal.valueOf(adateList.get(i).getEleTotel()).multiply(A_calibrationFactor);
                    BigDecimal bmultiply = BigDecimal.valueOf(bdateList.get(i).getEleTotel()).multiply(B_calibrationFactor);
                    BigDecimal cmultiply = BigDecimal.valueOf(cdateList.get(i).getEleTotel()).multiply(C_calibrationFactor);
                    BigDecimal add = amultiply.add(bmultiply).add(cmultiply);
                    DateNumListVo vo = new DateNumListVo();
                    vo.setDateTime(adateList.get(i).getDateTime());
                    vo.setNum(adateList.get(i).getNum());
                    vo.setEleTotel(add.doubleValue());
                    dateList.add(vo);
                }
                
                //List<DateNumListVo> dateList = lineChartMapper.getDateList(listVo.getDeviceNo(), listVo.getMonitoringStartTime());
                int accumulateMeteringDays = 0;
                Double eleTotel = 0.00;
                Double excelTotel = 0.00;
                Double energyLossTotel = 0.00;
                //Double parentDevExcelTotel= 0.00;
                // 获取当前设备的子级
                List<String> allChildrenIdWithParentId = new ArrayList<>();
                if(null != districtId){
                    allChildrenIdWithParentId = this.getAllChildrenIdWithParentId(listVo.getDeviceNo(), districtId);
                    List<String> newDeviceList = new ArrayList<>();
                    allChildrenIdWithParentId.stream().forEach(deviceNum -> {
                        String substring = deviceNum.substring(deviceNum.length() - 4, deviceNum.length());
                        newDeviceList.add(substring);
                    });
                }

                for (DateNumListVo dateNumListVo : dateList) {
                    if (dateNumListVo.getNum() == 96) {
                        accumulateMeteringDays = accumulateMeteringDays + 1;
                        BigDecimal b1 = new BigDecimal(Double.toString(eleTotel));
                        BigDecimal b2 = new BigDecimal(Double.toString(dateNumListVo.getEleTotel()));
                        eleTotel = b1.add(b2).doubleValue();

                        // 关联电表的电量和是根据导入的excel表格计算的，针对的也是“有效天
                        String deviceNo = listVo.getDeviceNo();
//                        String substring = deviceNo.substring(deviceNo.length() - 4, deviceNo.length());
//                        Double excelSelectTotal = tcpDataMapper.getExcelImportData(substring, dateNumListVo.getDateTime());
//                        if (null != excelSelectTotal) {
//                            BigDecimal b3 = new BigDecimal(Double.toString(excelTotel));
//                            BigDecimal b4 = new BigDecimal(Double.toString(excelSelectTotal));
//                            excelTotel = b3.add(b4).doubleValue();
//                        }
                        if(allChildrenIdWithParentId.size() != 0){
                            if(allChildrenIdWithParentId.size() != 1){
                                Map<String,Double> map = new HashMap<String,Double>();
                                Map<String,Double> meterMeteringMap = new HashMap<String,Double>();
                                for(String childDeviceNo: allChildrenIdWithParentId){
                                    // 判断是否是最低一级
                                    int count = tcpDataMapper.isFather(childDeviceNo);
                                    if(count == 0){
                                        // 获取该设备的电表信息以及设备电量和
                                        String substringDeviceNo = childDeviceNo.substring(childDeviceNo.length() - 4, childDeviceNo.length());
                                        Double onEleTotal = lineChartMapper.getDeviceEleTotalByDayAndOneDeviceNo(dateNumListVo.getDateTime(),substringDeviceNo);
                                        if(onEleTotal == null ){
                                            return;
                                        }
                                        Double dayOnEleTotal = lineChartMapper.getDayData(dateNumListVo.getDateTime(),childDeviceNo);
                                        if(dayOnEleTotal == null ){
                                            return;
                                        }

                                        BigDecimal bOnEleTotal = new BigDecimal(Double.toString(onEleTotal));
                                        BigDecimal bDayOnEleTotal = new BigDecimal(Double.toString(dayOnEleTotal));
                                        double dayChildeEnergyLoss = bDayOnEleTotal.subtract(bOnEleTotal).doubleValue();
                                        String parentDeviceNo = tcpDataMapper.getParentDeviveNo(childDeviceNo);
                                        Double aDouble = map.get(parentDeviceNo);
                                        if(aDouble == null && StringUtils.isNotEmpty(parentDeviceNo)){
                                            map.put(parentDeviceNo,dayChildeEnergyLoss);
                                        }
                                        if(aDouble != null && StringUtils.isNotEmpty(parentDeviceNo)){
                                            BigDecimal c1 = new BigDecimal(Double.toString(dayChildeEnergyLoss));
                                            BigDecimal c2 = new BigDecimal(Double.toString(aDouble));
                                            aDouble = c1.add(c2).doubleValue();
                                            map.put(parentDeviceNo,aDouble);
                                        }
                                        Double bDouble = meterMeteringMap.get(parentDeviceNo);
                                        if(bDouble == null && StringUtils.isNotEmpty(parentDeviceNo)){
                                            meterMeteringMap.put(parentDeviceNo,onEleTotal);
                                        }
                                        if(bDouble != null && StringUtils.isNotEmpty(parentDeviceNo)){
                                            //设备关联电表的累计电量
                                            BigDecimal c1 = new BigDecimal(Double.toString(onEleTotal));
                                            BigDecimal c2 = new BigDecimal(Double.toString(bDouble));
                                            aDouble = c1.add(c2).doubleValue();
                                            meterMeteringMap.put(parentDeviceNo,aDouble);
                                        }
                                    }
                                }
                                // 递归关联电表的累计数据
                                Map<String, Double> meterMeteringDoubleMap = toParentMap(meterMeteringMap, deviceNo);
                                Double aDouble1 = meterMeteringDoubleMap.get(listVo.getDeviceNo());
                                BigDecimal b5 = new BigDecimal(Double.toString(excelTotel));
                                BigDecimal b6 = new BigDecimal(Double.toString(aDouble1));
                                excelTotel = b5.add(b6).doubleValue();

                                // 递归累计数据
                                Map<String, Double> stringDoubleMap = toParentMap(map, deviceNo);
                                Double aDouble = stringDoubleMap.get(listVo.getDeviceNo());
                                BigDecimal b3 = new BigDecimal(Double.toString(energyLossTotel));
                                BigDecimal b4 = new BigDecimal(Double.toString(aDouble));
                                energyLossTotel = b3.add(b4).doubleValue();
                            }else{
                                String substringDeviceNo = deviceNo.substring(deviceNo.length() - 4, deviceNo.length());
                                Double onDayExcTotal = lineChartMapper.getDeviceEleTotalByDayAndOneDeviceNo(dateNumListVo.getDateTime(),substringDeviceNo);
                                if(onDayExcTotal != null ){
                                    BigDecimal childB1 = new BigDecimal(Double.toString(excelTotel));
                                    BigDecimal childB2 = new BigDecimal(Double.toString(onDayExcTotal));
                                    excelTotel = childB1.add(childB2).doubleValue();
                                }

                                // 计算最子级的电量损失
                                Double dayOnEleTotal = lineChartMapper.getDayData(dateNumListVo.getDateTime(),deviceNo);
                                if(dayOnEleTotal == null ){
                                    return;
                                }
                                if (null != dayOnEleTotal && null != onDayExcTotal) {
                                    BigDecimal childb1 = new BigDecimal(Double.toString(dayOnEleTotal));
                                    BigDecimal childb2 = new BigDecimal(Double.toString(onDayExcTotal));
                                    Double subtractRes = childb1.subtract(childb2).doubleValue();

                                    BigDecimal bigEnergyLossTotel = new BigDecimal(Double.toString(energyLossTotel));
                                    BigDecimal bigSubtractRes = new BigDecimal(Double.toString(subtractRes));
                                    energyLossTotel = bigEnergyLossTotel.add(bigSubtractRes).doubleValue();
                                }
                            }
                        }
                    }
                }
                // 监测设备的累计电量是指有效天的计量
                listVo.setAccumulateMeteringDays(accumulateMeteringDays);
                listVo.setDeviceMeteredElectricity(eleTotel);
                listVo.setElectricityMeterMetering(excelTotel);
                listVo.setEnergyLoss(energyLossTotel);
                // 电量损失率：电量损失/监测电量，保留两位小数
                if (null != listVo.getEnergyLoss() && null != eleTotel && 0 != eleTotel) {
                    double division = division(listVo.getEnergyLoss(), eleTotel, 2);
                    if (division == 0) {
                        listVo.setEnergyLossRate(0 + "%");
                    } else {
                        BigDecimal a1 = new BigDecimal(division).setScale(2, BigDecimal.ROUND_HALF_UP);
                        BigDecimal aa = new BigDecimal(100);
                        Double dd = a1.multiply(aa).doubleValue();
                        listVo.setEnergyLossRate(dd + "%");
                    }
                }
            }
        });

        return page;
    }

    @Override
    public Page<DeviceHistoryDataVo> getDeviceHistoryData(Map<String, Object> params) {
        Integer districtId = (Integer) params.get("districtId");
        String deviceNo = (String) params.get("deviceNo");

        if (districtId != null) {
            //DeviceTreeVo deviceTreeVo = lineChartMapper.getDistrictChild(queryDto.getDistrictId());
            // 判断是不是有三级目录
            // 获取台区名称
            String name = districtManageMapper.getName(Long.valueOf(districtId));
            if (name.equals(deviceNo)) {
                // 获取二级设备编号
                List<String> list = lineChartMapper.getCecondaryNo(deviceNo);
                // 获取父级为二级设备编号的
                Integer num = lineChartMapper.getDeviceNoNum(list);

                //若没有三级目录台区则展示空，万安台区下如果只有二级目录（没有根目录），默认进来为空，点击那个才显示那个
                if (0 == num) {
                    return new Page<>();
                }

                // 若有三级目录则进入页面未选择设备时，展示台区下第一个二级目录的数据和设备下边所有电表的和
                String newDeviceNo = list.get(0);
                params.put("deviceNo", newDeviceNo);
            }
        }

        // 获取设备校准系数
        BigDecimal A_calibrationFactor = new BigDecimal(1.00);
        BigDecimal B_calibrationFactor = new BigDecimal(1.00);
        BigDecimal C_calibrationFactor = new BigDecimal(1.00);

        String paramDeviceNo = (String) params.get("deviceNo");

        // 根据设备ID获取设备校准系数
        if(StringUtils.isNotEmpty(paramDeviceNo)){
            List<BigDecimal> calibrationFactorList = tcpDataMapper.getCalibrationFactor(paramDeviceNo);
            for (int i = 0; i < calibrationFactorList.size(); i++) {
                BigDecimal bigDecimal = calibrationFactorList.get(i);
                if(i == 0){
                    A_calibrationFactor = bigDecimal;
                }
                if(i == 1){
                    B_calibrationFactor = bigDecimal;
                }
                if(i == 2){
                    C_calibrationFactor = bigDecimal;
                }
            }
        }

        Query query = new Query(params);
        Page<DeviceHistoryDataVo> page = new Page<>(query);

        List<DeviceHistoryDataVo> list = lineChartMapper.getNewDeviceHistoryData(page, query);

        for (int i = 0; i < list.size(); i++) {

            BigDecimal AdatecCount = lineChartMapper.getADeviceHistoryData(list.get(i).getDateTime(),paramDeviceNo);

            BigDecimal BdatecCount = lineChartMapper.getBDeviceHistoryData(list.get(i).getDateTime(),paramDeviceNo);

            BigDecimal CdatecCount = lineChartMapper.getCDeviceHistoryData(list.get(i).getDateTime(),paramDeviceNo);

            BigDecimal amultiply = AdatecCount.multiply(A_calibrationFactor);
            BigDecimal bmultiply = BdatecCount.multiply(B_calibrationFactor);
            BigDecimal cmultiply = CdatecCount.multiply(C_calibrationFactor);
            BigDecimal add = amultiply.add(bmultiply).add(cmultiply);
            list.get(i).setDeviceMeteredElectricity(add.doubleValue());
        }

        //  2024/1/3 相序乘以校准系数

        // 获取设备名称
        Integer status = lineChartMapper.getDeviceStatus(query);

        // 获取台区名称
        DeviceHistoryDataVo vo = lineChartMapper.getDeviceInfo(query);

        // 获取当前子级的所有子级
        if (districtId == null) {
            if (null != vo) {
                districtId = Math.toIntExact(vo.getDistrictId());
            }
        }
        List<String> allChildrenIdWithParentId = new ArrayList<>();
        List<String> newDeviceList = new ArrayList<>();
        if (districtId != null){
            allChildrenIdWithParentId = this.getAllChildrenIdWithParentId(deviceNo, districtId);
            allChildrenIdWithParentId.stream().forEach(deviceNum -> {
                String substring = deviceNum.substring(deviceNum.length() - 4, deviceNum.length());
                newDeviceList.add(substring);
            });
            System.out.println(newDeviceList);
        }
        for(DeviceHistoryDataVo deviceHistoryDataVo :list){
            if (null != vo) {
                deviceHistoryDataVo.setDistrictName(vo.getDistrictName());
            }
            deviceHistoryDataVo.setDeviceStatus(status);

            if(allChildrenIdWithParentId.size() != 1){
                Map<String,Double> map = new HashMap<String,Double>();
                Map<String,Double> meterMeteringMap = new HashMap<String,Double>();
                for(String childDeviceNo: allChildrenIdWithParentId){
                    // 判断是否是最低一级
                    int count = tcpDataMapper.isFather(childDeviceNo);
                    if(count == 0){
                        // 获取该设备的电表信息以及设备电量和
                        String substring = childDeviceNo.substring(childDeviceNo.length() - 4, childDeviceNo.length());
                        Double onEleTotal = lineChartMapper.getDeviceEleTotalByDayAndOneDeviceNo(deviceHistoryDataVo.getDateTime(),substring);
                        if(onEleTotal == null ){
                            continue;
                        }
                        Double dayOnEleTotal = lineChartMapper.getDayData(deviceHistoryDataVo.getDateTime(),childDeviceNo);
                        if(dayOnEleTotal == null ){
                            continue;
                        }

                        BigDecimal bOnEleTotal = new BigDecimal(Double.toString(onEleTotal));
                        BigDecimal bDayOnEleTotal = new BigDecimal(Double.toString(dayOnEleTotal));
                        double v = bDayOnEleTotal.subtract(bOnEleTotal).doubleValue();

                        String parentDeviceNo = tcpDataMapper.getParentDeviveNo(childDeviceNo);
                        Double aDouble = map.get(parentDeviceNo);
                        if(aDouble == null && StringUtils.isNotEmpty(parentDeviceNo)){
                            map.put(parentDeviceNo,v);
                        }
                        if(aDouble != null && StringUtils.isNotEmpty(parentDeviceNo)){
                            // 电量损失
                            BigDecimal b1 = new BigDecimal(Double.toString(v));
                            BigDecimal b2 = new BigDecimal(Double.toString(aDouble));
                            aDouble = b1.add(b2).doubleValue();
                            map.put(parentDeviceNo,aDouble);
                        }
                        Double bDouble = meterMeteringMap.get(parentDeviceNo);
                        if(bDouble == null && StringUtils.isNotEmpty(parentDeviceNo)){
                            meterMeteringMap.put(parentDeviceNo,onEleTotal);
                        }
                        if(bDouble != null && StringUtils.isNotEmpty(parentDeviceNo)){
                            //设备关联电表的累计电量
                            BigDecimal c1 = new BigDecimal(Double.toString(onEleTotal));
                            BigDecimal c2 = new BigDecimal(Double.toString(bDouble));
                            aDouble = c1.add(c2).doubleValue();
                            meterMeteringMap.put(parentDeviceNo,aDouble);
                        }
                    }
                }
                // 递归关联电表的累计数据
                Map<String, Double> meterMeteringDoubleMap = toParentMap(meterMeteringMap, deviceNo);
                System.out.println(meterMeteringDoubleMap.get(deviceHistoryDataVo.getDeviceNo()));
                deviceHistoryDataVo.setElectricityMeterMetering(meterMeteringDoubleMap.get(deviceHistoryDataVo.getDeviceNo()));
                // 递归电量损失累计数据
                Map<String, Double> stringDoubleMap = toParentMap(map, deviceNo);
                System.out.println(stringDoubleMap.get(deviceHistoryDataVo.getDeviceNo()));
                deviceHistoryDataVo.setEnergyLoss(stringDoubleMap.get(deviceHistoryDataVo.getDeviceNo()));
            }else{
                Double eleTotal = lineChartMapper.getDeviceEleTotalByDayAndDeviceNo(deviceHistoryDataVo.getDateTime(), newDeviceList);
                deviceHistoryDataVo.setElectricityMeterMetering(eleTotal);
                if (null != deviceHistoryDataVo.getDeviceMeteredElectricity() && null != eleTotal) {
                    BigDecimal b1 = new BigDecimal(Double.toString(deviceHistoryDataVo.getDeviceMeteredElectricity()));
                    BigDecimal b2 = new BigDecimal(Double.toString(eleTotal));
                    double res = b1.subtract(b2).doubleValue();
                    //double res = Math.abs();
                    deviceHistoryDataVo.setEnergyLoss(res);
                    //deviceHistoryDataVo.setEnergyLoss(deviceHistoryDataVo.getDeviceMeteredElectricity() - eleTotal);
                }
            }

            if (null != deviceHistoryDataVo.getEnergyLoss() && null != deviceHistoryDataVo.getDeviceMeteredElectricity() && 0 != deviceHistoryDataVo.getDeviceMeteredElectricity()) {
                double division = division(deviceHistoryDataVo.getEnergyLoss(), deviceHistoryDataVo.getDeviceMeteredElectricity(), 2);
                if (division == 0) {
                    deviceHistoryDataVo.setEnergyLossRate(0 + "%");
                } else {
                    //BigDecimal a1 = new BigDecimal(division);
                    BigDecimal a1 = new BigDecimal(division).setScale(2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal aa = new BigDecimal(100);
                    Double dd = a1.multiply(aa).doubleValue();
                    deviceHistoryDataVo.setEnergyLossRate(dd + "%");
                }
            }
        }
        return page;
    }

    public Map<String,Double> toParentMap(Map<String,Double> map,String paramDeviceNo) {
        if(map.size() == 1 && map.get(paramDeviceNo) != null){
            return map;
        }
        Map<String,Double> parentMap = new HashMap<>();

        Double onEleTotal = 0.00;
        for(String key: map.keySet()){
            String parentDeviceNo = tcpDataMapper.getParentDeviveNo(key);
            if(map.get(parentDeviceNo) == null){
                map.put(parentDeviceNo,onEleTotal);
            }
            if(StringUtils.isNotEmpty(parentDeviceNo)){
                Double aDouble = map.get(key);
                Double aDouble1 = parentMap.get(parentDeviceNo);
                BigDecimal b1 = new BigDecimal(Double.toString(aDouble1));
                BigDecimal b2 = new BigDecimal(Double.toString(aDouble));
                double v = b1.add(b2).doubleValue();
                map.put(parentDeviceNo,v);
            }
        }
        if(parentMap.size() > 1 ){
            toParentMap(parentMap,paramDeviceNo);
        }
        return parentMap;
    }

    public List<String> getAllChildrenIdWithParentId(String treeId, Integer districtId) {
        List<DistrictTreeVo> trees = districtManageMapper.getDeviceById(Long.valueOf(districtId));
        List<DistrictTreeVo> childTrees = new ArrayList<>();
        // 递归
        this.treeOrgChildren(childTrees, trees, treeId);
        List<String> uuids = new ArrayList<>();
        for (DistrictTreeVo treeDTO : childTrees) {
            uuids.add(treeDTO.getDeviceNo());
        }
        uuids.add(treeId);
        return uuids;
    }

    /**
     * 获取指定菜单的所有子节点
     *
     * @param childTrees   子节点收集器
     * @param trees        当前菜单
     * @param parentTreeId 父菜单ID
     */
    private void treeOrgChildren(List<DistrictTreeVo> childTrees, List<DistrictTreeVo> trees, String parentTreeId) {
        for (DistrictTreeVo tree : trees) {
            if (tree.getParentId() != null && tree.getParentId().equals(parentTreeId)) {
                treeOrgChildren(childTrees, trees, tree.getDeviceNo());
                childTrees.add(tree);
            }
        }
    }

    @Override
    public R getNewLineChart(LineChartQueryDto queryDto) {
        if (queryDto.getDistrictId() != null) {
            //DeviceTreeVo deviceTreeVo = lineChartMapper.getDistrictChild(queryDto.getDistrictId());
            // 判断是不是有三级目录
            // 获取台区名称
            if (StringUtils.isEmpty(queryDto.getDeviceNo())) {
                String name = districtManageMapper.getName(queryDto.getDistrictId());
                // 获取二级设备编号
                List<String> list = lineChartMapper.getCecondaryNo(name);
                // 获取父级为二级设备编号的
                Integer num = lineChartMapper.getDeviceNoNum(list);

                //若没有三级目录台区则展示空，万安台区下如果只有二级目录（没有根目录），默认进来为空，点击那个才显示那个
                if (0 == num) {
                    return R.ok();
                }

                // 若有三级目录则进入页面未选择设备时，展示台区下第一个二级目录的数据和设备下边所有电表的和
                String deviceNo = list.get(0);
                queryDto.setDeviceNo(deviceNo);
            } else {
                String name = districtManageMapper.getName(queryDto.getDistrictId());
                if (name.equals(queryDto.getDeviceNo())) {
                    // 获取二级设备编号
                    List<String> list = lineChartMapper.getCecondaryNo(queryDto.getDeviceNo());
                    // 获取父级为二级设备编号的
                    Integer num = lineChartMapper.getDeviceNoNum(list);

                    //若没有三级目录台区则展示空，万安台区下如果只有二级目录（没有根目录），默认进来为空，点击那个才显示那个
                    if (0 == num) {
                        return R.ok();
                    }

                    // 若有三级目录则进入页面未选择设备时，展示台区下第一个二级目录的数据和设备下边所有电表的和
                    String deviceNo = list.get(0);
                    queryDto.setDeviceNo(deviceNo);
                }
            }
        }

        NewLineChartVo vo = new NewLineChartVo();
        List<NewLineChartDataVo> list = lineChartMapper.getNewLineChart(queryDto);
        vo.setTypeOne(list);
//        Double accumulate = 0.00;
//
//        List<NewLineChartDataVo> listTwo = new ArrayList<>();
//        for (NewLineChartDataVo newLineChartDataVo : list) {
//            BigDecimal b1 = new BigDecimal(Double.toString(accumulate));
//            BigDecimal b2 = new BigDecimal(Double.toString(newLineChartDataVo.getNum()));
//            accumulate = b1.add(b2).doubleValue();
//
//            NewLineChartDataVo twoVo = new NewLineChartDataVo();
//            twoVo.setDateTime(newLineChartDataVo.getDateTime());
//            twoVo.setNum(accumulate);
//            listTwo.add(twoVo);
//        }
//        vo.setTypeTwo(listTwo);
//        String deviceNo = queryDto.getDeviceNo();
//        String substring = deviceNo.substring(deviceNo.length() - 4, deviceNo.length());
//        queryDto.setDeviceNo(substring);
//        Double excelTotal = lineChartMapper.getExcelImportTotal(queryDto);
//        vo.setImpoortTotal(excelTotal);
        return CommonUtils.msg(vo);
    }

    /**
     * 2个数相除
     *
     * @param v1
     * @param v2
     * @param scale 保留几位小数
     * @return
     */
    public static double division(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
